/**
 * All rights Reserved, Designed By 信鸥科技
 * project : Innovation-design
 * Created by youquer on 17/11/9.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */

import React from 'react';
import { Table, Pagination } from 'antd';
import styles from './index.less';

/**
 * SmartTable:参数说明
 * advanceSelect:用处:高级搜索参数 类型:数组Array, 说明:左侧显示搜索名称 *
 * 例: const advanceSelect = [{name:"创建时间",inputName:"creatTime"},{name:"结束时间",inputName:"endTime"}];
 * select: 用处:高级搜索 类型:对象, fistValue, secondValue *
 * 例: let select = {fistValue: "姓名",secondValue : "性别"};
 * columns: 用处:Table, 参数: key,title,dataIndex,render, 具体用法参考:ant.design的table组件
 * data: 数组, table的数据源dataSource,
 * loading: 加载
 * current: page的当前页
 * total: page数据总数量
 * parentType: model的namespace,table父组件Model的namespace
 * table:
 * addpathname: 添加按钮跳转路径
 * dispatch: action的dispatch
 * type: namepsace
 * upDateAction: 分页走网方法
 * */
const SmartTable = ({ columns, data, loading, page, total, type, upDateAction, scroll, top, dispatch, stateArray, noPage }) => {
  /**
   * onShowSizeChange: 改变分页size
   * current: 改变size后当前页
   * size: 改变后分页size
   * */
  function onShowSizeChange(current, size) {
    dispatch({
      type: type + '/save',
      payload: {
        page: current,
        pageSize: size
      }
    });
    console.log(current, size);
    dispatch({
      type: type + upDateAction,
    });
  }

  /**
   * onChange: 改变page
   * page: 改变后page
   * size: 分页size
   * */
  function onChange(page, pageSize){
    dispatch({
      type: type + '/save',
      payload: {
        page: page,
        pageSize: pageSize,
      }
    });
    dispatch({
      type: type + upDateAction,
    });
  }

  function handleTableChange(pagination, filters, sorter){
    dispatch({
      type: type + '/save',
      payload: filters
    });
    dispatch({
      type: type + '/fetch',
      payload: filters
    })
  }

  var pagination = [];
  if (!noPage){
    pagination.push(
      <Pagination
        key="pagination-key"
        className={styles.pagination}
        showSizeChanger={true}
        showQuickJumper={true}
        showSizeChanger={true}
        defaultCurrent={10}
        pageSizeOptions={['10', '20', '30', '40']}
        onShowSizeChange={onShowSizeChange}
        onChange={onChange}
        current={page}
        total={total}
        showTotal={(total) => ('共有' + total + '条数据')} />
    );
  }

  return (
    <div>
      <Table
        onChange={handleTableChange}
        className={styles.table_clear}
        style={{top: top}}
        loading={loading}
        dataSource={data}
        columns={columns}
        rowKey={record => record.id}
        pagination={false}
        scroll={scroll}
        footer={() => (
          <div className={styles.footer}>
            {pagination}
          </div>
        )}
      />
    </div>
  );
};
export default SmartTable;
